package com.example.juc.big_talk_concurrent.chapter6.article1;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Test2 {
    static Runnable task = new Runnable() {
        @Override
        public void run() {
            System.out.println("准备开启屏障");
        }
    };
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2, task);

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("子线程到达屏障");
                    cyclicBarrier.await(); // 子线程已达到屏障
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("child Thread");
            }
        }).start();

        System.out.println("主线程到达屏障");
        cyclicBarrier.await(); // 主线程已到达屏障
        System.out.println("main Thread");
    }
}
